/******************************************************************************
 * @File name   :      AuthInteceptor.java
 *
 * @Author      :      wujianxi
 *
 * @Date        :      2011-9-24
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2011-9-24 ä¸Šå�ˆ11:49:24        wujianxi     1.0            Initial Version
 * 2011-10-13 ä¸‹å�ˆ4:45:46        Wu Jianxi     1.1           refactor code
 *****************************************************************************/
package com.cap.bts.framework.auth.interceptor;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cap.bts.framework.auth.util.UserAuthenticationUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.cap.bts.framework.common.util.CookieUtils;
import com.cap.bts.framework.usermgnt.domain.Function;
import com.cap.bts.framework.usermgnt.service.IUserMgntService;
import com.cap.bts.framework.usermgnt.util.UserMgntUtil;

/**
 *
 */
@Component
public class FunctionAuthInterceptor extends HandlerInterceptorAdapter {

    private static final String REQUEST_AUTH_FAILED = "/auth/funcAuthFailed";
    
    private static final String QUESTION_MARK = "?";

   // @Autowired
    //private IUserMgntService userMgntService;
    
    
    private Map<String,Integer> timeoutUrlMap;
    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2011-9-24
     * @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
    **/
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
        Object handler) throws Exception {
//        
//        List<Function> allFunctionList = userMgntService.getAllFunctions();
//        
//        String requestURI = request.getRequestURI();
//        if(StringUtils.isBlank(requestURI)){
//            return true;
//        }
//        
//        String funcUrl = requestURI.substring(request.getContextPath().length());
//        String simpleFuncUrl = funcUrl;
//        //remove query parameter
//        if(funcUrl.lastIndexOf(QUESTION_MARK) > -1){
//            simpleFuncUrl = simpleFuncUrl.substring(0, simpleFuncUrl.lastIndexOf(QUESTION_MARK));
//        }
//        
//        if(funcUrl.startsWith(REQUEST_AUTH_FAILED) || !UserMgntUtil.isRequestInFunctionList(funcUrl, allFunctionList)){
//            return true;
//        }
//        
//        String userCode = CookieUtils.getCookie(request, response, "user");
//        if(StringUtils.isBlank(userCode)){
//            return true;
//        }
//        List<Function> grantedFunctionList = userMgntService.getFunctionsByUserCode(userCode);
//        
//        //å¦‚æžœç”¨æˆ·è®¿é—®çš„URLéœ€è¦�è¿›è¡Œæ�ƒé™�æŽ§åˆ¶çš„Function Listï¼Œåˆ™åˆ¤æ–­è¯¥ç”¨æˆ·æ˜¯å�¦èƒ½å¤Ÿè®¿é—®è¯¥URL
//        if(!UserMgntUtil.isRequestInFunctionList(funcUrl, grantedFunctionList)){
//            response.sendRedirect(request.getScheme() + "://" + request.getServerName() + ":" 
//                + request.getServerPort() + request.getContextPath() + REQUEST_AUTH_FAILED);
//            return false;
//        }
//        boolean neverExpiredFlag = false;
//        for(Map.Entry<String, Integer> entry: timeoutUrlMap.entrySet()){
//            if(simpleFuncUrl.startsWith(entry.getKey())){
//                neverExpiredFlag = true;
//                UserAuthenticationUtils.setLoginToCookie(request, response, userCode, entry.getValue());
//            }
//        }
//        if(!neverExpiredFlag){
//            UserAuthenticationUtils.setLoginToCookie(request, response, userCode);
//        }
//        
        return true;
    }
    /**
     * @Date        :      2011-10-12
     *
     * @return the userMgntService
     */
//    public IUserMgntService getUserMgntService() {
//        return userMgntService;
//    }
//    /**
//     * @Date        :      2011-10-12
//     *
//     * @param userMgntService the userMgntService to set
//     */
//    public void setUserMgntService(IUserMgntService userMgntService) {
//        this.userMgntService = userMgntService;
//    }
    /**
     * @Date        :      2011-10-12
     *
     * @return the timeoutUrlMap
     */
    public Map<String, Integer> getTimeoutUrlMap() {
        return timeoutUrlMap;
    }
    /**
     * @Date        :      2011-10-12
     *
     * @param timeoutUrlMap the timeoutUrlMap to set
     */
    public void setTimeoutUrlMap(Map<String, Integer> timeoutUrlMap) {
        this.timeoutUrlMap = timeoutUrlMap;
    }
    
}
